package com.example.newsapp.view.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.newsapp.R;
import com.example.newsapp.model.entity.NewsItem;
import com.example.newsapp.view.activity.NewsDetailActivity;

import java.util.ArrayList;
import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
    private List<NewsItem> newsList = new ArrayList<>();

    @NonNull
    @Override
    public NewsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
        return new NewsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewsViewHolder holder, int position) {
        NewsItem newsItem = newsList.get(position);
        holder.titleTextView.setText(newsItem.getTitle());
        holder.sourceTextView.setText(newsItem.getSource());
        holder.timeTextView.setText(newsItem.getPtime());
        holder.commentTextView.setText(String.valueOf(newsItem.getReplyCount()) + "评");

        Glide.with(holder.itemView.getContext())
                .load(newsItem.getImgsrc())
                .placeholder(R.drawable.image_placeholder)
                .error(R.drawable.image_error)
                .into(holder.imageView);

        // 添加点击事件
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), NewsDetailActivity.class);
            intent.putExtra("url", newsItem.getUrl());
            intent.putExtra("title", newsItem.getTitle());
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return newsList.size();
    }

    public void setNewsList(List<NewsItem> newsList) {
        this.newsList = newsList;
        notifyDataSetChanged();
    }

    public void addNewsList(List<NewsItem> moreNews) {
        int startPos = this.newsList.size();
        this.newsList.addAll(moreNews);
        notifyItemRangeInserted(startPos, moreNews.size());
    }

    static class NewsViewHolder extends RecyclerView.ViewHolder {
        TextView titleTextView;
        TextView sourceTextView;
        TextView timeTextView;
        TextView commentTextView;
        ImageView imageView;

        public NewsViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.textViewTitle);
            sourceTextView = itemView.findViewById(R.id.textViewSource);
            timeTextView = itemView.findViewById(R.id.textViewTime);
            commentTextView = itemView.findViewById(R.id.textViewComment);
            imageView = itemView.findViewById(R.id.imageViewNews);
        }
    }
}